﻿#region File and License Information
/*
<File>
	<License Type="BSD">
		Copyright © 2009 - 2012, Daniel Vaughan. All rights reserved.
	
		This file is part of Calcium (http://calciumsdk.net).

		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are met:
			* Redistributions of source code must retain the above copyright
			  notice, this list of conditions and the following disclaimer.
			* Redistributions in binary form must reproduce the above copyright
			  notice, this list of conditions and the following disclaimer in the
			  documentation and/or other materials provided with the distribution.
			* Neither the name of the <organization> nor the
			  names of its contributors may be used to endorse or promote products
			  derived from this software without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
		DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
		DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
		(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
		ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	</License>
	<Owner Name="Daniel Vaughan" Email="danielvaughan@outcoder.com" />
	<CreationDate>2010-10-08 13:01:06Z</CreationDate>
</File>
*/
#endregion

using System;

namespace DanielVaughan
{
	public class LockedOperations
	{
		/// <summary>
		/// Tries to set the specified value to <c>true</c> 
		/// if it is not already <c>true</c>. Uses the specified valueLock
		/// as a memory barrier if the specified value is <c>false</c>.
		/// </summary>
		/// <param name="value">If set to <c>true</c>, the value will not be changed.</param>
		/// <param name="valueLock">The value lock.</param>
		/// <returns><c>true</c> if the value 
		/// was set to <c>true</c>; otherwise <c>false</c>.</returns>
		public static bool TrySetTrue(ref bool value, object valueLock)
		{
			ArgumentValidator.AssertNotNull(valueLock, "valueLock");
			if (!value)
			{
				lock (valueLock)
				{
					if (!value)
					{
						value = true;
						return true;
					}
				}
			}
			return false;
		}

		/// <summary>
		/// Tries to set the specified value to <c>false</c> 
		/// if it is not already <c>false</c>. Uses the specified valueLock
		/// as a memory barrier if the specified value is <c>true</c>.
		/// </summary>
		/// <param name="value">If set to <c>false</c>, the value will not be changed.</param>
		/// <param name="valueLock">The value lock.</param>
		/// <returns><c>true</c> if the value 
		/// was set to <c>false</c>; otherwise <c>false</c>.</returns>
		public static bool TrySetFalse(ref bool value, object valueLock)
		{
			ArgumentValidator.AssertNotNull(valueLock, "valueLock");
			if (value)
			{
				lock (valueLock)
				{
					if (value)
					{
						value = false;
						return true;
					}
				}
			}
			return false;
		}

		/// <summary>
		/// Performs the specified <c>Action</c> if the specified value
		/// is <c>true</c>. Uses the specified valueLock
		/// as a memory barrier, when performing the action.
		/// </summary>
		/// <param name="value">if set to <c>true</c> 
		/// then the specified Action will be performed.</param>
		/// <param name="valueLock">Used for thread safety when performing 
		/// the specified Action.</param>
		/// <param name="action">The action to perform.</param>
		/// <returns><c>true</c> if the action was performed; 
		/// otherwise <c>false</c> is returned.</returns>
		/// <exception cref="ArgumentNullException">
		/// Thrown if the specified action is <c>null</c>.</exception>
		/// <exception cref="Exception">
		/// Occurs if the specified action throws an exception.</exception>
		public static bool DoIfTrue(ref bool value, object valueLock, Action action)
		{
			ArgumentValidator.AssertNotNull(action, "action");
			ArgumentValidator.AssertNotNull(valueLock, "valueLock");
			if (value)
			{
				lock (valueLock)
				{
					if (value)
					{
						action();
						return true;
					}
				}
			}
			return false;
		}

		/// <summary>
		/// Performs the specified <c>Action</c> if the specified value
		/// is <c>true</c>. Uses the specified valueLock
		/// as a memory barrier, when performing the action.
		/// </summary>
		/// <param name="value">if set to <c>true</c> 
		/// then the specified Action will be performed.</param>
		/// <param name="valueLock">Used for thread safety when performing 
		/// the specified Action.</param>
		/// <param name="action">The action to perform.</param>
		/// <returns><c>true</c> if the action was performed; 
		/// otherwise <c>false</c> is returned.</returns>
		/// <exception cref="ArgumentNullException">
		/// Thrown if the specified action is <c>null</c>.</exception>
		/// <exception cref="Exception">
		/// Occurs if the specified action throws an exception.</exception>
		public static bool DoIfFalse(ref bool value, object valueLock, Action action)
		{
			ArgumentValidator.AssertNotNull(action, "action");
			ArgumentValidator.AssertNotNull(valueLock, "valueLock");
			if (!value)
			{
				lock (valueLock)
				{
					if (!value)
					{
						action();
						return true;
					}
				}
			}
			return false;
		}

		/// <summary>
		/// Does if true.
		/// </summary>
		/// <param name="getIfTrueFunc">The func to determine 
		/// whether to perform the specified action.</param>
		/// <param name="valueLock">Used for thread safety when performing 
		/// the specified Action.</param>
		/// <param name="action">The action to perform.</param>
		/// <returns><c>true</c> if the action was performed; 
		/// otherwise <c>false</c> is returned.</returns>
		/// <exception cref="ArgumentNullException">
		/// Thrown if either action or getIfTrueFunc is <c>null</c>.</exception>
		/// <exception cref="Exception">
		/// Occurs if the specified action throws an exception.</exception>
		public static bool DoIfTrue(
			Func<bool> getIfTrueFunc, object valueLock, Action action)
		{
			ArgumentValidator.AssertNotNull(getIfTrueFunc, "getIfTrueFunc");
			ArgumentValidator.AssertNotNull(action, "action");

			if (getIfTrueFunc())
			{
				lock (valueLock)
				{
					if (getIfTrueFunc())
					{
						action();
						return true;
					}
				}
			}
			return false;
		}
	}
}
